package com.yangcangclub.commons.velocity.directive;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

import java.io.IOException;
import java.io.Writer;

/**
 * 自定义Velocity指令(#ifnotnull)
 * @author HumnChou
 *
 */
public class Ifnotnull extends Directive {
	
	public String getName() {
		return "ifnotnull";
	}
	
	public int getType() {
		return BLOCK;
	}

	public boolean render(InternalContextAdapter context, Writer writer,Node node) throws IOException, ResourceNotFoundException,
			ParseErrorException, MethodInvocationException {
		Object value = node.jjtGetChild(0).value(context);
		if (value != null) {
			if(!String.valueOf(value).equals("")){
				Node content = node.jjtGetChild(1);
				content.render(context, writer);
			}
		}
		return true;
	}
}
